package praktikumid.p05_t;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * Second homework example.
 * @author ago
 *
 */
public class FeedBeta {
	
	/**
	 * Target language.
	 */
	public static String lang = "en";
	/**
	 * Cache size in days.
	 */
	public static int cacheSize = 0;
	/**
	 * Sort order. Could also be boolean (true - ascending, false - descending)
	 */
	public static String sortOrder = null;
	
	public static final String GOOGLE_GROUPS_URL = "https://groups.google.com/group/iti0011/feed/rss_v2_0_msgs.xml";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		// järgmine kord: regexp
		/*
		 * todo list:
		 * 
		 * käsurea parameetrite parsimine
		 *  > void parseArgs(String[] args)
		 * 
		 * java Feed en 7 desc
		 * java Feed desc en 7
		 * 
		 * java Feed --lang=en --cache-size=7 --sort=desc
		 * java Feed -l en -c 7 -s d
		 * 
		 * loeme Google Groups XML
		 *  > String readGoogleGroups()
		 * 
		 * XML-i töötlemine:
		 *   - jagate postitusteks (<item></item>)
		 *   - igast postitusest väljad:
		 *     - title
		 *     - description
		 *     - pubDate
		 * 1)
		 *  > String[] parseGoogleGroupsXML(String xml)
		 * 2)
		 *  > String[] getItems(String xml)
		 *  > for s in items: String parseGGItem(String item)
		 * 
		 * tõlkimine
		 *  > String[] translate(String title, String desc, String lang)
		 *  > String translate(String text, String lang)
		 *  
		 *    kasutada ka google groups puhul:
		 *    > String readURL(String url)
		 * 
		 * postituste printimine
		 *   for i in items: print i
		 */
		
		parseArgs(args);
		String xml = readURL(GOOGLE_GROUPS_URL);
		//System.out.println(xml);
		GoogleItem item = parseGoogleGroups(xml);
		
		System.out.println(item.title);
		
		// anna tõlkimisse (item.title)
		// anna tõlkimisse (item.description)
		
		
	}
	
	public static void parseArgs(String[] args) {
		// TODO: to implement
		lang = "fi";
		cacheSize = 5;
	}

	public static String readURL(String address) {
		try {
			URL url = new URL(address);
			URLConnection c = url.openConnection();
			c.setRequestProperty("User-agent", "Mozilla/5.0");
			
			BufferedReader br = new BufferedReader(
					new InputStreamReader(c.getInputStream())
					);
			String line;
			String xml = "";
			while ((line = br.readLine()) != null) {
				xml += line;
				//System.out.println(line);
			}
			return xml;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static GoogleItem parseGoogleGroups(String xml) {
		/*
		 * "tere tulemast".indexOf("t") = 0
		 * "tere tulemast".indexOf("t", 1) = 5
		 * "tere tulemast".indexOf("tu") = 5
		 * "tere tulemast".indexOf("tur") = -1
		 * 
		 * 
		 * "tere tulemast".substring(0, 1) = "t"
		 * 0 - kaasa arvatud
		 * 1 - kuni (index < 1)
		 * "tere tulemast".substring(5, 7) = "tu"
		 */
		// String.indexOf
		// String.substring
		// <title>tere</title>
		
		String tag = "<title>";
		String title = "";
		int itemStartPos = xml.indexOf("<item>");
		if (itemStartPos > -1) {
			// leidsime item'i
			int titleStartPos = xml.indexOf(tag, itemStartPos);
			if (titleStartPos > -1) {
				titleStartPos += tag.length();
				int titleEndPos = xml.indexOf("</title>", titleStartPos);
				title = xml.substring(titleStartPos, titleEndPos);
			}
		}
		System.out.println(title);
		GoogleItem i = new GoogleItem();
		i.title = title;
		
		
		GoogleItem j = new GoogleItem();
		j.title = "tere";
		
		GoogleItem.blah = "tere";
		
		//
		
		GoogleItem.blah = "head aega";
		
		return i;
		
		/*
		 * Auto a = new Auto();
		 * a.värv = "punane";
		 * a.mark = "Audi";
		 * 
		 * Auto b = new Auto();
		 * b.värv = "Kollane";
		 * b.mark = "Lada";
		 * 
		 */
		
	}
}